package com.jxnu.escms.Control.BinControl;

import com.github.pagehelper.Page;
import com.jxnu.escms.Control.UserControl.UserControl;
import com.jxnu.escms.Pojo.PageResult;
import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.BinService.BinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;


/**
 * @author yihui
 * 回收站模块对应后台代码
 */
@Slf4j
@RestController
public class BinControl {
    @Resource
    private BinService binService;

    @Resource
    private UserControl userControl;

    @RequestMapping("/deleted")
    public Map<String, Object> getDeletedUser(@RequestBody Map<String, Integer> page){
        Integer start = page.get("start");
        Integer pageSize = page.get("pageSize");
        PageResult<User> pages = binService.getDeletedUsers(start, pageSize);

        Page<User> pages1 = pages.getPages();
        for(User p: pages1){
            String address = userControl.getAddress(p, p.getId());
            p.setAddress(address);
            List list = userControl.parseAddress(address);
            p.setComName((String) list.get(0));
            p.setBuild((String) list.get(1));
            p.setNuit((String) list.get(2));
            p.setDoor((String) list.get(3));

            p.setHealthCode(p.getHealthCode() + "码");
        }

        Map<String, Object> res= new HashMap<>();
        res.put("res", pages);
        return res;
    }

    @RequestMapping("/kill")
    public Map<String, String> kill(@RequestBody User user){
        // 删除此用户
        int i = binService.killUser(user.getId());
        Map<String, String> res = new HashMap<>();
        res.put("kill", i == 1? "success":"worry");
        return res;
    }

    @RequestMapping("/restore")
    public Map<String, String> restore(@RequestBody Map<String, Object> ids){
        Integer adminId = (Integer) ids.get("adminId");
        List<Integer> userId = (List<Integer>)ids.get("userId");

        int i =0 ;

        for (int p : userId){
            binService.restartUser(adminId, p, "还原");
            i++;
        }

        Map<String, String> res= new HashMap<>();
        if(i == userId.size()){
            res.put("restore", "success");
        }
        else{
            res.put("restore", "worry");
            // 添加有误就全部删除
            userId.forEach(p->{
                binService.restartUser(adminId, p, "删除");
            });
        }
        return res;
    }

    @RequestMapping("/check")
    public Map<String, Object> check(@RequestBody Map<String, String> data){
        String name = data.get("name");
        String build = data.get("build");
        String nuit = data.get("nuit");

        List<User> res = new ArrayList<>();

        List<User> allDeletedUsers = binService.getAllDeletedUsers();
        for(User p : allDeletedUsers){
            String realName = p.getRealName();
            if(!Objects.equals(name, "") && Objects.equals(name, realName)){
                res.add(p);
            }
            else if(Objects.equals(name, "")){
                res.add(p);
            }
        }
        List<User> res1 = new ArrayList<>();

        for(User p: res){
             List list = userControl.parseAddress(userControl.getAddress(p, p.getId()));
             p.setComName((String) list.get(0));
             p.setBuild((String) list.get(1));
             p.setNuit((String) list.get(2));
             p.setDoor((String) list.get(3));
             p.setHealthCode(p.getHealthCode() + "码");

             String b= (String) list.get(1);

             if(!(!Objects.equals(build, "") && !Objects.equals(build, b))){
                 res1.add(p);
             }
        }

        List<User> res2 = new ArrayList<>();
        for(User p: res1){
            String n= p.getNuit();

            if(!(!Objects.equals(nuit, "") && !Objects.equals(nuit, n))){
                res2.add(p);
            }
        }

        Map<String, Object> r = new HashMap<>();
        r.put("data", res2);
        r.put("total", res2.size());
        return r;
    }

}
